
var baseUrl = 'http://192.168.31.190:8088/bonus/';
var wsUrl = 'ws://192.168.31.190:8088/bonus/'; //test git

// var baseUrl = 'http://122.114.190.246:8080/bonus/';
// var wsUrl = 'ws://122.114.190.246:8080/bonus/';

var okCode = '0000';
var paramErrorCode = '0001';

//获取dom元素
var $ = function (select, dom) {
    if (dom) {
        var result = dom.querySelectorAll(select);
        if (result.length == 1) {
            return result[0];
        } else {
            return result;
        }
    } else {
        var result = document.querySelectorAll(select);
        if (result.length == 1) {
            return result[0];
        } else {
            return result;
        }
    }
}

//获取原素的样式
function getStyle(attr) {
    if (this.currentStyle) {//ie  
        return this.currentStyle[attr];
    } else {
        return getComputedStyle(this)[attr];
    }
}

// 设置CSS变量
function setCssParam(cssPro, cssVal) {
    if (cssPro.indexOf('--') < 0) {
        cssPro = '--' + cssPro;
    }
    document.body.style.setProperty(cssPro, cssVal);
}

// 获取CSS变量
function getCssParam(cssPro) {
    if (cssPro.indexOf('--') < 0) {
        cssPro = '--' + cssPro;
    }
    return document.body.style.getPropertyValue(cssPro);
}

// 为元素添加事件监听
function addMyEventListener(arr, type, f) {
    if (arr.length && arr.length > 1) {
        Array.prototype.forEach.call(arr, function (item, i) {
            item[type] = f;
        });
    } else {
        arr[type] = f;
    }

}

// 获取元素兄弟节点
function getMySiblings(elm) {
    var a = [];
    var p = elm.parentNode.children;
    for (var i = 0, pl = p.length; i < pl; i++) {
        if (p[i] !== elm) a.push(p[i]);
    }
    return a;
}

function getMyInputListObj(list) {
    var obj = {};
    for (var i = 0; i < list.length; i++) {
        obj[list[i].getAttribute('name')] = list[i].value.trim();
    }
    obj.ssid = 'loginSsid' + obj.nickOrMobile;
    return obj;
}

// 获取发生某种事件并且添加过该事件监听的对象
function getMyEventTarget(e, type) {
    var tar = e.target;
    while (tar && !tar['on' + type]) {
        tar = tar.parentNode;
    }
    return tar;
}

function validAinB(a, b) {
    if (b.length) {
        for (var i = 0; i < b.length; i++) {
            if (a == b[i]) {
                return true;
            }
        }
        return false;
    } else {
        return a == b;
    }
}

function getMyEventMeetsRule(e, rule) {
    var tar = e.target;
    var ruleNode = $(rule);
    while (tar && !validAinB(tar, ruleNode)) {
        tar = tar.parentNode;
    }
    return tar;
}

function hasMyClass(obj, cls) {
    if (!obj.className) {
        return false;
    }
    return obj.className.indexOf(cls) >= 0;
}

function addMyClass(obj, cls) {
    if (!hasMyClass(obj, cls)) obj.className += " " + cls;
}

function removeMyClass(obj, cls) {
    if (hasMyClass(obj, cls)) {
        var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
        obj.className = obj.className.replace(reg, ' ');
    }
    return obj;
}

function toggleMyClass(obj, cls) {
    if (hasMyClass(obj, cls)) {
        removeMyClass(obj, cls);
    } else {
        addMyClass(obj, cls);
    }
}

var Ajax = {
    get: function (url, fn, fnError) {
        showTempPageOneTop(allPage.loading);
        if (url.indexOf('http') < 0) {
            url = baseUrl + url;
        }
        var obj = new XMLHttpRequest();  // XMLHttpRequest对象用于在后台与服务器交换数据          
        obj.open('GET', url, true);
        obj.onreadystatechange = function () {
            if (obj.readyState == 4) {
                hideOnePage(allPage.loading);
            }
            if (obj.readyState == 4 && obj.status == 200 || obj.status == 304) { // readyState==4说明请求已完成
                fn.call(this, obj.responseText);  //从服务器获得数据
            } else if (obj.readyState == 4) {
                if (fnError) {
                    fnError.call(this, obj.responseText);
                } else {
                    alert('服务器繁忙，请稍后再试');
                }
            }
        };
        //obj.send(null);
    },
    post: function (url, data, fn, fnError) {
        showTempPageOneTop(allPage.loading);
        if (url.indexOf('http') < 0) {
            url = baseUrl + url;
        }
        var obj = new XMLHttpRequest();
        obj.open("POST", url, true);
        obj.setRequestHeader("Content-type", "application/json"); // 发送信息至服务器时内容编码类型
        obj.onreadystatechange = function () {
            if (obj.readyState == 4) {
                hideOnePage(allPage.loading);
            }
            if (obj.readyState == 4 && (obj.status == 200 || obj.status == 304)) {  // 304未修改
                console.log("received data:" + obj.responseText);
                fn.call(this, obj.responseText);
            } else if (obj.readyState == 4) {
                if (fnError) {
                    console.log("received data:" + obj.responseText);
                    fnError.call(this, obj.responseText);
                } else {
                    alert('服务器繁忙，请稍后再试');
                }
            }
        };
        if (!(typeof str == 'string')) {
            data = JSON.stringify(data);
        }
        console.log("send data:" + data);
        obj.send(data);
    }
}



function viewPageBottom() {
    document.getElementsByTagName('BODY')[0].scrollTop = document.getElementsByTagName('BODY')[0].scrollHeight;
}
function viewPageTop() {
    document.getElementsByTagName('BODY')[0].scrollTop = 0;
}
// 显示某个页面(其他已显示的页面会被隐藏)
function showOnePage(page) {
    for (var one in allPage) {
        if (allPage[one] != page) {
            addMyClass(allPage[one], 'displayOff');
        }
    }
    removeMyClass(page, 'displayOff');
}

// 隐藏一个页面（不影响其他页面）
function hideOnePage(page) {
    removeMyClass(page, 'zindexMax');
    addMyClass(page, 'displayOff');
}
// 显示一个页面在顶部（不隐藏其他已显示的页面）
function showTempPageOneTop(page) {
    removeMyClass(page, 'displayOff');
    addMyClass(page, 'zindexMax');
}

//复制一个对象的属性
function copyObjFields(target, orgin) {
    for (var attr in orgin) {
        target[attr] = orgin[attr];
    }
}

//获取一个元素的sample
function getElementSample(id) {
    var ele = $('#' + id).cloneNode(true);
    removeMyClass(ele, 'displayoff');
    ele.removeAttribute('id');
    return ele;
}

//时间长转化为格式时间
function timeFormat(time, format) {
    var dt = new Date(time);
    return dt.format(format);
}


Date.prototype.format = function (format) {
    var date = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        "S+": this.getMilliseconds()
    };
    if (/(y+)/i.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    for (var k in date) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1
                ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
        }
    }
    return format;
}